#include <iostream>
#include <array>
#include <vector>
#include <unordered_set>

using namespace std;

static uint64_t fuck(uint64_t num) {
    const vector<uint64_t> v {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};
    uint64_t res = 0;
    while (num) {
        res += v[num % 10];
        num /= 10;
    }
    return res;
}

int main() {
    constexpr unsigned limit = 1'000'000;
    unsigned ans = 0;
    array<int, limit> arr {0};
    for (unsigned i = 1; i < limit; ++i) {
        unordered_set<uint64_t> s {i};
        uint64_t n = i;
        do {
            n = fuck(n);
            if (n < arr.size() && arr[n] != 0) {
                arr[i] = s.size() + arr[n];
                if (arr[i] == 60) {
                    ++ans;
                }
                break;
            } else if (s.find(n) == s.end()) {
                s.insert(n);
            } else {
                arr[i] = s.size();
                break;
            }
        } while (true);
    }
    cout << ans << endl;
}